package com.jxbeidou.system.runner;

import com.jxbeidou.core.cache.CacheService;
import com.jxbeidou.core.constents.CacheConstants;
import com.jxbeidou.system.menu.service.RoleMenuService;
import com.jxbeidou.system.setting.entity.IgnoreUrlDO;
import com.jxbeidou.system.setting.service.IgnoreUrlService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

import jakarta.annotation.Resource;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @program: jxbeidou-basic
 * @ClassName: SystemApplicationRunner
 * @description: 白名单数据获取
 * @author: 杜晓祥
 * @create: 2023-12-28 11:11
 * 版权所有：交信北斗(北京)信息科技有些公司
 */
@Component
@Slf4j
public class SystemIgnoreUrlApplicationRunner implements ApplicationRunner {

    @Resource
    private CacheService cacheService;

    @Resource
    private IgnoreUrlService ignoreUrlService;


    @Override
    public void run(ApplicationArguments args) throws Exception {
        log.info("[白名单预加载]开始加载访问白名单数据");
        cacheService.del(CacheConstants.SYS_LOGIN_IGNORE_LIST);
        List<IgnoreUrlDO> list = ignoreUrlService.list();
        if (ObjectUtils.isEmpty(list)) {
            log.warn("[白名单预加载]没有找的访问白名单数据");
            log.warn("[白名单预加载]系统启动后，登录接口无法访问");
            return;
        }
        Set<String> urlSet = list.stream().map(e -> e.getUrl()).collect(Collectors.toSet());
        cacheService.sAdd(CacheConstants.SYS_LOGIN_IGNORE_LIST, urlSet.toArray(new java.lang.String[0]));
        log.info("[白名单预加载]白名单加载成功,size:[{}]", urlSet.size());
    }
}
